1294B - Collecting Packages - CodeForces Solution


implementation sortings *1200

Please click on ads to support us..

Python Code:

def input_int():
    return int(input())


def input_int_list():
    return [int(_) for _ in input().split()]


def input_str():
    return input()


def input_str_list():
    return [_ for _ in input().split()]


def print_solution(solution_list):
    for solution in solution_list:
        print(solution)


def print_solution_list(solution_list):
    for solution in solution_list:
        out = ''
        for i in solution:
            out += str(i) + ' '
        print(out)


if __name__ == "__main__":
    solution_list = []
    t = input_int()

    for _ in range(t):
        n = input_int()
        xy_map = {}

        for __ in range(n):
            x, y = map(int, input().split())
            xy_map.setdefault(x, []).append(y)

        map_key = list(xy_map.keys())
        map_key.sort()

        c_x = 0
        c_y = 0
        s = ''
        for key in map_key:
            if key > c_x:
                s += 'R' * (key - c_x)
                c_x = key
            value = xy_map[key]
            value.sort()
            if value[0] < c_y:
                solution_list.append(['NO'])
                break
            s += 'U' * (value[-1] - c_y)
            c_y = value[-1]
        else:
            solution_list.append(['YES'])
            solution_list.append([s])

    print_solution_list(solution_list)

 	      			  				 				  			  	

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int main(){
  int t;
  cin >> t;

  while(t--){
    int n;
    cin >> n;

    pair<int,int> p[n];
    for(int i = 0; i < n; i++){
      cin >> p[i].first >> p[i].second;
    }

    sort(p, p+n);

    bool test = true;
    for(int i = 0; i+1 < n; i++){
      if(p[i].second > p[i+1].second){
	test = false;
	break;
      }
    }

    if(!test){
      printf("NO\n");
      continue;
    }

    printf("YES\n");

    string moves = "";
    int x = 0, y = 0;

    for(int i = 0; i < n; i++){
      while(x < p[i].first){
	moves.push_back('R');
	x++;
      }
      while(y < p[i].second){
	moves.push_back('U');
	y++;
      }
    }

    printf("%s\n", moves.c_str());
  }

  return 0;
}


Comments

Submit
0 Comments
More Questions

152. Maximum Product Subarray
337. House Robber III
869. Reordered Power of 2
1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set